package com.leshua.channel.mpos.model.base;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;

/**
 * Created by ZHANGWEN878 on 2017-04-20.
 */
public class JsonResult {
    private Integer code ;
    private String errorCode ;
    private String errorMessage ;
    private String message ;
    private Object data ;

    private long count ;

    //未登录
    public static final String ERRORCODE_NOTLOGIN = "10000" ;
    //需要验证码
    public static final String ERRORCODE_NEED_AUTHCODE = "10010" ;
    //验证码不正确
    public static final String ERRORCODE_ERROR_AUTHCODE = "10011" ;
    //是否需要改密码
    public static final String ERRORCODE_IS_DANGE = "10020" ;
    //是否需要短信验证
    public static final String ERRORCODE_IS_NEED_SM_CHECK = "10030" ;
    //登录多个代理商
    public static final String ERRORCODE_LOGIN_TOO_MUCH = "10040" ;
    //暂未开放
    public static final String ERRORCODE_TEMPORARILY_NOT_OPENED = "10050";


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setCode(CODE value ){
        this.code = value.getValue() ;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static JsonResult successResult(Object data){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.SUCCESS);
        jr.setData(data);
        return jr ;
    }

    public static JsonResult successResult(Object data, long count){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.SUCCESS);
        jr.setData(data);
        jr.setCount(count);
        return jr ;
    }

    public static JsonResult successResult(Page<?> pageInfo){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.SUCCESS);
        jr.setData(null);
        jr.setCount(0);
        if (pageInfo != null) {
        	jr.setData(pageInfo.getResult());
            jr.setCount(pageInfo.getTotal());
		}
        return jr ;
    }

    public static JsonResult successResult(){
        return successResult(null);
    }

    public static JsonResult excptionResult(String message){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.EXCEPTION);
        jr.setErrorMessage(message);
        jr.setMessage(message);
        return jr ;
    }
    
    public static JsonResult errorResult(String errorMessage, Object data){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.ERROR);
        jr.setErrorCode(String.valueOf(CODE.ERROR.getValue()));
        jr.setErrorMessage(errorMessage);
        jr.setMessage(errorMessage);
        jr.setData(data==null?"":data);
        return jr ;
    }

    public static JsonResult errorResult(String errorCode, String errorMessage, Object data){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.ERROR);
        jr.setErrorCode(errorCode);
        jr.setErrorMessage(errorMessage);
        jr.setMessage(errorMessage);
        jr.setData(data);
        return jr ;
    }

    public static JsonResult notLoginResult(String errorMessage, Object data){
        JsonResult jr = new JsonResult();
        jr.setCode(CODE.ERROR);
        jr.setErrorCode(ERRORCODE_NOTLOGIN);
        jr.setErrorMessage(errorMessage);
        jr.setMessage(errorMessage);
        jr.setData(data);
        return jr ;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public enum CODE{
        SUCCESS(0),ERROR(-1),EXCEPTION(-1);

        private CODE(Integer value){
            this.value = value ;
        }

        private Integer value ;

        public Integer getValue() {
            return value;
        }
    }
}
